package queue;

/**
 * @author chtw
 * @date 2020-09-05 16:48
 */
public class LoopQueue {
    private int head;
    private int tail;
    private int maxSize;
    private int[] arr;

    public LoopQueue(int maxSize) {
        this.head = this.tail = 0;
        this.maxSize = maxSize;
        this.arr = new int[maxSize];
    }

    public boolean isFull() {
        return (this.tail + 1) % this.maxSize == this.head;
    }

    public boolean IsEmpty() {
        return this.head == this.tail;
    }

    public int Size() {
        return (this.tail + this.maxSize - this.head) % this.maxSize;
    }

    public boolean push(int value) {
        if (this.isFull()) {
            return false;
        }
        this.arr[this.tail] = value;
        this.tail = (this.tail + 1) % this.maxSize;
        return true;
    }

    public Integer pop() {
        if (this.IsEmpty()) {
            return null;
        }
        int value = this.arr[this.head];
        this.head = (this.head + 1) % this.maxSize;
        return value;
    }

    public void list() {
        if (IsEmpty()) {
            System.out.println("队空~");
            return;
        }
        int temp = this.head;
        for (int i = 0; i < this.Size(); i++) {
            System.out.printf("arr[%d]=%d\n",temp, this.arr[temp]);
            temp = (temp + 1) % this.maxSize;
        }
    }
}
